﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Collections.Generic;

namespace GoShopping.Web.SystemManager
{
    public partial class SysManagerIndex : System.Web.UI.Page
    {
        BLL.UserBean ub = new GoShopping.BLL.UserBean();
        BLL.MemberShipService ms = new GoShopping.BLL.MemberShipService();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //Master.PrdtButton.Visible = false;
                //Master.SupplierButton.Visible = false;
                //Master.ElementSelectedChange = GetTreeView;
                GetGVData(null);
            }
        }
        private void GetTreeView()
        {
            TreeView tv = Master.MasterTreeView;
            SetControlCommon.GetPrdtTreeView(tv, Server.MapPath("~/ProductManager/Navigation.xml"), "sys");
        }
        protected void btnUnLock_Click(object sender, EventArgs e)
        {
            //LockOrUnLockUser(true);
            Response.Redirect("LockedUsers.aspx");
        }

        protected void btnSendMessage_Click(object sender, EventArgs e)
        {
            Response.Redirect("MessageResponse.aspx");
        }

        protected void btnSendMail_Click(object sender, EventArgs e)
        {
            Response.Redirect("SendEmail.aspx");
        }

        protected void extGVUser_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.extGVUser.PageIndex = e.NewPageIndex;
            GetGVData(null);
        }

        private void GetGVData(string filterString)
        {
            IList<MembershipUser> iUser;
            if (string.IsNullOrEmpty(filterString))
            {
                iUser = ms.GetRoleUsers(GoShopping.Model.Enumerations.Member_Roles.Role_Member);
            }
            else
            {
                iUser=ms.GetMembers(filterString);

            }
            this.extGVUser.DataSource = iUser;
            this.extGVUser.DataBind();

            #region temp
            //MembershipUser msu = new MembershipUser();
            //string str1 = msu.CreationDate;
            //string str2 = msu.Email;
            //string str3 = msu.IsApproved;
            //string str4 = msu.IsLockedOut;
            //string str5 = msu.IsOnline;
            //string str6 = msu.LastActivityDate;
            //string str7 = msu.LastLockoutDate;
            //string str8 = msu.LastLoginDate;
            //string str9 = msu.UserName;
            //for (int i = 0; i < iUser.Count; i++)
            //{
            //    if (iUser[i] == false)
            //    {
            //        iUser.RemoveAt(i);
            //        i--;
            //    }
            //}
            #endregion
        }

        protected void extGVUser_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "EditItem")
            {
                //Response.Redirect("" + e.CommandArgument);
            }
        }

        protected void cbAll_CheckedChanged(object sender, EventArgs e)
        {
            // 遍历数据集的所有数据项
            foreach (GridViewRow row in this.extGVUser.Rows)
            {
                // 取得数据项行首的CheckBox，使其Checked属性与表头的CheckBox同步
                CheckBox subCB = (CheckBox)row.FindControl("cbSingle");
                subCB.Checked = ((CheckBox)sender).Checked;
            }
        }

        protected void btnLock_Click(object sender, EventArgs e)
        {
            LockOrUnLockUser(false);
        }

        private void LockOrUnLockUser(bool isLock)
        {
            // 使用ArrayList记录选择的员工编号
            ArrayList selList = new ArrayList();
            int i = 0;
            // 遍历数据集
            foreach (GridViewRow row in this.extGVUser.Rows)
            {
                // 获取行首的CheckBox，判断是否为Checked
                CheckBox cb = (CheckBox)row.FindControl("cbSingle");
                if (cb.Checked == false)
                {
                    continue;
                }
                //// 若为Checked，将信息添加到ArrayList
                selList.Add(((Label)row.FindControl("lblName")).Text);
                i++;
            }
            // 若遍历完成后ArrayList为空，则表示未选中任何项，页面提示
            if (selList.Count == 0)
            {
                return;
            }
            BLL.MemberShipService ms = new GoShopping.BLL.MemberShipService();
            //执行锁定操作
            for (int j = 0; j < selList.Count; j++)
            {
                ms.ApproveMember(selList[j].ToString(), isLock);
            }
            // 重新绑定员工列表
            GetGVData(null);
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            GetGVData(zltxtSearchCondition.Text.Trim());
        }

        public string ChangeToChinese(object obj)
        {
            if (Convert.ToBoolean(obj))
            {
                return "是";
            }
            else
            {
                return "否";
            }
        }
        #region Del User
        //protected void btnDel_Click(object sender, EventArgs e)
        //{
        //    // 使用ArrayList记录选择的员工编号
        //    ArrayList selList = new ArrayList();
        //    int i = 0;
        //    // 遍历数据集
        //    foreach (GridViewRow row in this.extGVUser.Rows)
        //    {
        //        // 获取行首的CheckBox，判断是否为Checked
        //        CheckBox cb = (CheckBox)row.FindControl("cbSingle");
        //        if (cb.Checked == false)
        //        {
        //            continue;
        //        }
        //        //// 若为Checked，将信息添加到ArrayList
        //        selList.Add(Convert.ToInt16(((Label)row.FindControl("lblID")).Text));
        //        i++;
        //    }
        //    // 若遍历完成后ArrayList为空，则表示未选中任何项，页面提示
        //    if (selList.Count == 0)
        //    {
        //        return;
        //    }
        //    // 调用逻辑方法，执行删除操作
        //    for (int j = 0; j < selList.Count; j++)
        //    {
        //        //dtb.DelDicTag(Convert.ToInt16(selList[j]));
        //    }
        //    // 重新绑定员工列表
        //    GetGVData(null);
        //}

        //private void GetGVData(string filterString)
        //{
        //    if (string.IsNullOrEmpty(filterString))
        //    {
        //        IList<Model.UserInfo> iUser = ub.GetAllUserList();
        //        this.extGVUser.DataSource = iUser;
        //        this.extGVUser.DataBind();
        //    }
        //    else
        //    {
        //        IList<Model.UserInfo> iUser = ub.GetUserList(filterString);
        //        this.extGVUser.DataSource = iUser;
        //        this.extGVUser.DataBind();
        //    }

        //}
        #endregion
    }
}
